package ai.people.netmon.msgsearch.service.impl;

import ai.people.netmon.framework.domain.searchmsg.bean.EsUserTotalFields;
import ai.people.netmon.framework.domain.searchmsg.request.KeyAccountSearchRequest;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.msgsearch.domain.entity.EsUserTotal;
import ai.people.netmon.msgsearch.service.KeyAccountService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhenglin
 * @date 2022/8/1
 */
@Service
@Slf4j
public class KeyAccountServiceImpl implements KeyAccountService {

    @Autowired
    @Qualifier("elasticsearchRestTemplate")
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Override
    public PageResponse<EsUserTotalFields> search(KeyAccountSearchRequest request) {
        String searchKeywords = request.getSearchKeywords();
        // 创建查询对象
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(
                new BoolQueryBuilder()
//                        .should(new MatchQueryBuilder("city",searchKeywords))
                        .should(new MatchQueryBuilder("user_name",searchKeywords))
                        .should(new MatchQueryBuilder("user_id",searchKeywords))
                        .should(new MatchQueryBuilder("id",searchKeywords))
//                        .should(new MatchQueryBuilder("description",searchKeywords))
                        .should(new MatchQueryBuilder("urls",searchKeywords))
                        .should(new MatchQueryBuilder("email",searchKeywords))
//                        .should(new MatchQueryBuilder("hometown",searchKeywords))
//                        .should(new MatchQueryBuilder("address",searchKeywords))
                        .should(new MatchQueryBuilder("work",searchKeywords))
                        .should(new MatchQueryBuilder("phone",searchKeywords))
                        .should(new MatchQueryBuilder("nick_name",searchKeywords))
        );

        builder.withQuery(boolQueryBuilder);
        builder.withPageable(PageRequest.of(request.getPageNo(), request.getPageSize()));

        NativeSearchQuery query = builder.build();
        query.setTrackTotalHits(true);

        // 调用查询接口
        SearchHits<EsUserTotal> searchHits = null;
        try {
//            log.info("es query:{}",query.getQuery().toString());
            searchHits = elasticsearchRestTemplate.search(query, EsUserTotal.class, IndexCoordinates.of("user-*"));
        }catch (Exception e){
            e.printStackTrace();
        }
        List<EsUserTotalFields> data = new ArrayList<>();
        if(searchHits != null){
            List<SearchHit<EsUserTotal>> hits = searchHits.getSearchHits();
            for (SearchHit<EsUserTotal> hit : hits) {
                EsUserTotal content = hit.getContent();
                EsUserTotalFields parseObject = JSONObject.parseObject(JSONObject.toJSONString(content)).toJavaObject(EsUserTotalFields.class);
                data.add(parseObject);
            }
            long totalHits = searchHits.getTotalHits();
            return PageResponse.builder(data,totalHits);
        }
        return PageResponse.builderEmpty();
    }
}
